import { Component } from '@angular/core';
import { NavController,AlertController } from 'ionic-angular';
import { HTTP } from '@ionic-native/http';
import $ from "jquery";
import {AboutPage} from "../about/about";//全局
declare var Alipay: any;//支付宝支付
declare var Wechat: any; // 此处声明plugin.xml中clobbers对应的值。
@Component({
  selector: 'page-chong',
  templateUrl: 'chong.html'
})
export class ChongPage {
 
  

  constructor(public navCtrl: NavController,private http: HTTP,private malert:AlertController) {
      this.can()
  }
  
/**
 * 支付宝支付
 */
      AliTopay(){
        // alert(localStorage.getItem("userChong"));
      //  console.log("充值金额" + localStorage.getItem("userChong"));
        console.log('吊起支付');
          this.http.post("http://118.31.79.20:8080/Stock/alipay/appAlipay.do", {"money":localStorage.getItem("userChong"),"uid":localStorage.getItem("userid")}, {})
          .then(data => {  
            console.log('支付返回信息')
            // console.log('支付'+data.data);
            let  parameters=data.data; 
           
            Alipay.Base.pay(parameters, ()=> {
              console.log('信息成功');  
                //   let alert= this.malert.create({
                //   title :"提示",
                //   message:"支付成功",        
                // });
                //  alert.present();
                //  console.log('支付'+parameters);
                this.navCtrl.push(AboutPage);
                
            },(error) =>{
              console.log('信息失败')
                let alert= this.malert.create({
                  title :"提示",
                  message:"支付失败",
                  
                });
              alert.present();

            });
            // Alipay.Base.pay(parameters)
            //       .then(res=>{
            //           console.log('新对'+  res);
            //       },error=>{
            //           console.log('新错'+error);
            //       })
            //       .catch(e=>{
            //         console.log('新失败'+e);
            //       })  
          })
          .catch(error => {
            let alert= this.malert.create({
                  title :"提示",
                  message:"支付失败",                  
                });
              alert.present();

          });
      }



      /**http://116.62.153.81:8080/fashion2/guocui/wxPrePay.html
    微信支付
   * 请求支付信息
   */
        wxReqPay(){
          // alert(localStorage.getItem("userChong"));
        //  console.log("充值金额" + localStorage.getItem("userChong"));
        console.log('吊起支付');
          this.http.get("http://118.31.79.20:8080/Stock/xiaomai/wxPrePay.do", {"hhNum":localStorage.getItem("userChong"),"userid":localStorage.getItem("userid")}, {})
            .then(data => {
              // alert("请求成功");
             console.log('请求成功');
              let data_data=JSON.parse(data.data);
            let root= data_data.msg;
            let  params = {
                mch_id: root.partnerid, // merchant id
                prepay_id: root.prepayid, // prepay id returned from server
                nonce: root.noncestr, // nonce string returned from server
                timestamp:root.timestamp, // timestamp
                sign: root.sign, // signed string
              };

            this.wxToPay(params);
            })
            .catch(error => {
             let alert= this.malert.create({
                  title :"提示",
                  message:"网络异常",       
                });
              alert.present();


            }).catch();


        }

        /**
   * 微信支付
   */
  wxToPay(params){
  
   Wechat.sendPaymentRequest(params, ()=>{
     let alert= this.malert.create({
                  title :"提示",
                  message:"支付成功",                 
                });
              alert.present();

           
       }, (error)=>{
           let alert= this.malert.create({
                  title :"提示",
                  message:"支付失败",                
                });
              alert.present();

       });

  }
  can(){
     console.log('充值'+localStorage.getItem("userid"));
    $(".chong-qian").hover(function(){
        $(this).css({"background":"red","color":"#fff"});

    },function(){

        $(this).css({"background":"#fff","color":"#000"});
    });

     $(".chong-qian").click(function(){
         localStorage.setItem("userChong",$(this).val());
        console.log("充值金额" + localStorage.getItem("userChong"));
     })


  }
   ionViewWillEnter(){
  
    this.can();


   }
 /**
    * 自定义弹框
    */
    mWind(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,
      buttons:[{
        text:'取消',
        handler:()=>{         
        }
      },{
        text:'确定',
        handler:()=>{          
        }
      }],
      
    });
  alert.present();

    }

}